void handle_interrupt(isr_stack state) {
    int n = state.interrupt;
    if(n > IRQ0) {
		externalSignal(n+1);
    }
    else if(n == IRQ0) {
		update_msclock();
		reset_pic(n);
		start_interrupts();
    }
	else if(n == 13 && state.eflags & EFLAGS_VM) {
		if(v86_monitor(&state))
			start_interrupts();
		else
			kernel_panic(state);
	}
	else {
		kernel_panic(state);
	}
}

void kernel_panic(isr_stack state) {
	start_interrupts();
	go_text_mode();
	sleep(2000);
	debug_isr_stack(state);
	exit(1);
}

void reset_pic(int interrupt_number) {
    if(interrupt_number >= IRQ8) {
        outb(0xA0, 0x20);
    }
    outb(0x20, 0x20);
}

void sleep(int milliseconds) {
	printf("I'm feeling sleepy.\n");
	int end_time = millisecond_clock + milliseconds;
	while(millisecond_clock < end_time) {
		halt_cpu();
	}
}

void exit(int error_number) {
	go_text_mode();
    printf("VM Exited: %d", error_number);
    stop_interrupts();
    halt_cpu();
}

int force_exception() {
    volatile int dividend = 0;
    return 1 / dividend;
}
